package com.example.newinfinity.BackgroundImage

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout.LayoutParams
import android.widget.ImageView
import android.widget.PopupWindow
import com.bumptech.glide.Glide
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup
import com.example.newinfinity.R
import com.example.newinfinity.databinding.BgPickerViewBinding

class BgImageManager(context: Context) {
    //背景图片弹窗视图xml的Binding的创建
    private var bgPickerViewBinding: BgPickerViewBinding? = null

    //记录当前选中状态的背景图片
    private var mSelectedBgImage: BgImageModel? = null

    //给DrawView提供一个回调事件
    //用于点击某个背景图片然后响应将该背景图片显示到界面上去
    var bgImageChangeCallback: (Bitmap) -> Unit = {}

    private val bgPickerViewPopupWindow: PopupWindow by lazy {
        bgPickerViewBinding = BgPickerViewBinding.inflate(LayoutInflater.from(context))

        bindRecyclerView()  //创建PopupWindow视图时配置其中recyclerView中的数据
        PopupWindow().apply {
            this.contentView = bgPickerViewBinding!!.root
            this.width = LayoutParams.WRAP_CONTENT
            this.height = LayoutParams.WRAP_CONTENT
        }
    }

    //配置背景图片弹窗的recyclerView
    private fun bindRecyclerView() {
        bgPickerViewBinding!!.recyclerView.linear().setup {
            addType<BgImageModel>(R.layout.bg_picker_view_item)
            onBind { //填充数据
                //获取每个item的xml文件中的图片用于填充数据
                val model = getModel<BgImageModel>()  //获取每一个背景图片的model
                val imageResId = model.resId
                val imageBitmap = BitmapFactory.decodeResource(
                    context.resources,
                    imageResId
                )  //根据资源id 获取到具体的图片bitmap
                val bg = findView<ImageView>(R.id.image_bitmap)  //得到每个item中的xml视图的图片
                //bg.setImageBitmap(imageBitmap)  //赋于其具体的图片bitmap
                Glide.with(context).load(imageBitmap).into(bg)  //Glide缓存图片 加载更顺畅

                //点击图片就设置为选中状态
                bg.setOnClickListener {
                    if (mSelectedBgImage == null) { //暂无选中图形
                        mSelectedBgImage = model
                        model.clickState = ClickState.SELECTED
                        bgImageChangeCallback(imageBitmap) //将当前的bitmap回调给DrawView 用于将该图片显示到界面上去
                    } else { //已有选中图形
                        if (mSelectedBgImage != model) { //判断是否重复点击同一个图片
                            mSelectedBgImage!!.clickState = ClickState.NORMAL
                            mSelectedBgImage = model
                            mSelectedBgImage!!.clickState = ClickState.SELECTED
                            bgImageChangeCallback(imageBitmap) //将当前的bitmap回调给DrawView 用于将该图片显示到界面上去
                        }
                    }
                    notifyDataSetChanged()
                }

                //只有选中的图片才能显示选中的边框
                if (model.clickState == ClickState.SELECTED) {
                    findView<View>(R.id.click_bg).visibility = View.VISIBLE
                } else {
                    findView<View>(R.id.click_bg).visibility = View.INVISIBLE
                }
            }
        }.models = getBgImageData()
    }

    //背景图片弹窗的消失与出现
    fun showBgImagePopupWindow(view: View, x: Int, y: Int) {
        bgPickerViewPopupWindow.showAsDropDown(view,x,y)
    }

    fun hideBgImagePopupWindow() {
        bgPickerViewPopupWindow.dismiss()
    }
}